package dht

type Bucket struct {
	tables map[string]ID
}

func (b *Bucket) Len() int {
	return len(b.tables)
}

func (b *Bucket) Put(id ID, k int) {
	if len(b.tables) == k {
		return
	}
	if b.tables == nil {
		b.tables = make(map[string]ID)
	}
	b.tables[id.String()] = id
}

func (b *Bucket) Del(id ID) {
	delete(b.tables, id.String())
}

func (b *Bucket) Has(id ID) bool {
	_, ok := b.tables[id.String()]
	return ok
}

func (b *Bucket) Each(fn func(id ID) bool) bool {
	for _, id := range b.tables {
		flag := fn(id)
		if flag == false {
			return false
		}
	}
	return true
}

func (b *Bucket) List() []ID {
	list := make([]ID, 0, len(b.tables))
	for _, id := range b.tables {
		list = append(list, id)
	}
	return list
}
